<?php
  /**
   * iJPageEdit
   *
   * The iJPageEdit class provides Page Editing
   *
   * @copyright Copyright (c) 2011, xEngine Progect
   */
    class PageEdit extends iJBlock
    {
        /**
         *
         * class construct
         *
         *
         * @access public
         */
        public function __construct() {
        }
        
        /**
         * execute
         *
         * Executes the block to generate the tokens
         *
         * @return array Array with the tokens of the block
         *
         * @access public
         */
        public function execute() {
          $iJDataBase  = iJFactory::singleton('iJDataBase');
          $iJLanguages = iJFactory::singleton('iJLanguages');
          $iJBlock     = iJFactory::singleton('iJBlock');
          $iJPages     = iJFactory::singleton('iJPages');
          if(isset($_POST['create'])) {
            $iJPages::createPage($_POST); 
          }
          
          if(isset($_GET['key']) && !empty($_GET['key'])) {
            $pageInfo   = $iJPages->getPageByKeyword($_GET['key']);
            if(isset($pageInfo['id'])) {
              $pageBlocks = $iJPages->getPageBlocksById($pageInfo['id']);
              $this->addToken('pageInfo',    $pageInfo);
              $this->addToken('pageBlocks',  $pageBlocks);
            } else {
              //TODO redirect class
              //iJRedirect(iJHrefLink::linkToLocalPage('admin_pages'), 3);
              iJMessages::display('warning', 'page_not_found');
            }
            
          }
         
          $this->addToken('blocks',       $iJBlock->getBlocks());
          $this->addToken('preview_link', iJHrefLink::linkToLocalPage('pages_preview')); 
          $this->addToken('form_action',  iJHrefLink::linkToLocalPage('admin_pages_new')); 
          $this->addToken('pages_list',   iJHrefLink::linkToLocalPage('admin_pages'));
          $this->addToken('curLanguage',  $iJLanguages->getLanguage());
          $this->addToken('languages',    $iJLanguages->getLanguagesExeptCurrent());
   
          return parent::execute();
        }
        
    }
?>